package com.itheima.controller.admin;

import com.itheima.common.result.PageResult;
import com.itheima.common.result.Result;
import com.itheima.dto.SysUserDTO;
import com.itheima.dto.SysUserPageQueryDTO;
import com.itheima.service.SysUserService;
import com.itheima.vo.SysUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/sys_user")
@Api(tags = "管理员管理")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 用户列表分页
     *
     * @param dto
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "用户列表分页")
    public Result<PageResult> page(SysUserPageQueryDTO dto) {

        log.info("用户分页：{}", dto);
        return sysUserService.pageQuery(dto);
    }

    /**
     * 根据id删除用户=====没有采用物理删除方法，而是禁用了状态，并且把删除标记开启 设为1
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    @ApiOperation(value = "根据id删除用户")
    public Result deleteById(String id) {

        log.info("根据id删除用户:{}", id);

        return sysUserService.deleteById(id);

        //物理删除方法
//        return Result.success(sysUserService.removeById(id));
    }

    /**
     * 添加管理员
     *
     * @param dto
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加管理员")
    public Result addSysUser(@RequestBody SysUserDTO dto) {

        log.info("添加管理员:{}", dto);

        return sysUserService.addSysUser(dto);

    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    @ApiOperation(value = "根据id查询")
    public  Result<SysUserVO> selectById(String id) {
        log.info("根据id查询:{}", id);
        return sysUserService.getSysUserById(id);
    }

    /**
     * 修改管理员信息
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改管理员信息")
    public Result update(@RequestBody Map map) {
        return sysUserService.updateByMap(map);
    }
}
